package artid.com.arttogoa.adapter;

import java.util.List;
import org.apache.commons.lang3.StringUtils;
import artid.com.arttogoa.R;
import artid.com.arttogoa.barcode.Cache;
import artid.com.arttogoa.parser.MySellParser;
import artid.com.arttogoa.parser.ReturnOneStateParser;
import artid.com.arttogoa.util.AlertUtils;
import artid.com.arttogoa.util.NetUtils;
import artid.com.arttogoa.util.ToastUtil;
import artid.com.arttogoa.volley.VolleyBasic;
import artid.com.arttogoa.volley.VolleyCallback;
import artid.com.arttogoa.volley.VolleyData;
import artid.com.arttogoa.volley.VolleyMap;
import com.android.volley.VolleyError;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.target.BitmapImageViewTarget;
import com.nostra13.universalimageloader.core.ImageLoader;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * 
 *    
 * 项目名称：ArtworkArtA   
 * 类名称   ：MySell_Adapter   
 * 类描述   ：我的出售适配器   
 * 创建人   ：耿卫斌   
 * 创建时间：2016-7-18 上午10:23:51   
 *
 */
public class MySell_Adapter extends BaseAdapter{

	private Activity mContext;
	private int  whichActivity=0;//从哪个页面的适配器(0:出售中 1：已下架)                                                               
	private List<MySellParser> list;
	private Drawable drawable;

	public MySell_Adapter(Activity mContext,List<MySellParser> list, int whichActivity) {
		super();
		this.mContext = mContext;
		this.whichActivity = whichActivity;
		this.list = list;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		ViewHolder holder = null;
		View v = convertView;
		if (v == null) {
			holder = new ViewHolder();
			v = LayoutInflater.from(mContext).inflate(R.layout.item_mysell, null);
			holder.img=(ImageView) v.findViewById(R.id.item_mysell_img);
			holder.title=(TextView) v.findViewById(R.id.item_mysell_title);
			holder.time=(TextView) v.findViewById(R.id.item_mysell_time);
			holder.isguarantee=(TextView) v.findViewById(R.id.item_mysell_isguarantee);
			holder.price=(TextView) v.findViewById(R.id.item_mysell_price);
			holder.commendNum=(TextView) v.findViewById(R.id.item_mysell_commendNum);
			holder.goodsClick=(TextView) v.findViewById(R.id.item_mysell_goodsClick);
			holder.deleteBtn=(TextView) v.findViewById(R.id.item_mysell_delete);
			v.setTag(holder);
		} else {
			holder = (ViewHolder) v.getTag();
		}
		Glide.with(mContext).load(list.get(position).getGoods_image()
				+ "_"+ (Integer.parseInt(NetUtils.getDisplayMetrics((Activity)mContext).get(NetUtils.DM_WIDTH)) /1) + "x10.png").
				asBitmap().placeholder(R.drawable.yspba_img_mor).error(R.drawable.yspba_img_mor)
				.into(new BitmapImageViewTarget(holder.img));
		holder.title.setText(list.get(position).getRealname()+"《"+list.get(position).getGoods_name()+"》");
		holder.time.setText(list.get(position).getGoods_addtime());
		holder.price.setText("￥"+list.get(position).getGoods_price());
		if(!StringUtils.isEmpty(list.get(position).getGoods_commend_num())){
			holder.commendNum.setText(list.get(position).getGoods_commend_num());
		}else{
			holder.commendNum.setText("0");
		}
		if(!StringUtils.isEmpty(list.get(position).getGoods_click())){
			holder.goodsClick.setText(list.get(position).getGoods_click());
		}else{
			holder.goodsClick.setText("0");
		}
		if(whichActivity==0){
			//出售中适配器
			holder.deleteBtn.setVisibility(View.GONE);
			drawable=mContext.getResources().getDrawable(R.drawable.wdcs_ico_bao_sel);
			drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight()); //设置边界
			holder.isguarantee.setCompoundDrawables(drawable, null,null, null);//画在左边
			holder.price.setTextColor(Color.parseColor("#FE1F09"));
		}else if(whichActivity==1){
			//已下架适配器
			holder.deleteBtn.setVisibility(View.VISIBLE);
			drawable=mContext.getResources().getDrawable(R.drawable.wdcs_ico_bao_gray);
			drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight()); //设置边界
			holder.isguarantee.setCompoundDrawables(drawable, null,null, null);//画在左边
			holder.price.setTextColor(Color.parseColor("#9B9B9B"));
		}
		if("1".equals(list.get(position).getIs_auth())){
			//已备案
			holder.isguarantee.setVisibility(View.VISIBLE);
		}else{
			holder.isguarantee.setVisibility(View.GONE);
		}
		holder.deleteBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				showDeleteDialog(position);
			}
		});
		return v;
	}
	class ViewHolder {
		ImageView img;
		TextView title,time,isguarantee,price,commendNum,goodsClick,deleteBtn;
	}

	/**
	 * 商品上/下架/删除(我出售的详情使用相同)
	 * 1删除 ，2下架，3上架
	 */
	private void goodsControl(String good_ID,final int position){
		VolleyBasic.getInstance(mContext).post(Cache.otherGoods,
				VolleyMap.getInstance().otherGoods(good_ID,"1"),
				ReturnOneStateParser.class, new VolleyCallback() {
			@Override
			public void onSuccess(VolleyData vd) {
				// TODO Auto-generated method stub
				System.out.println(vd.getJson());
				ReturnOneStateParser mReturnOneStateParser=(ReturnOneStateParser) vd.getData();
				if("1".equals(mReturnOneStateParser.getState())){
					//成功
					list.remove(position);
					notifyDataSetChanged();
					if(list!=null || list.size()==0){
						// 通知刷新列表（删除相对应的商品）
						Intent intent = new Intent("com.MySell_fragment.receiver");
						intent.putExtra("intentActivity", "我出售的已下架-删除");
						mContext.sendBroadcast(intent);
					}
				}else if("0".equals(mReturnOneStateParser.getState())){
					//失败
					ToastUtil.toast(mContext, "删除失败");
				}
			}
			public void onError(VolleyError volleyError) {
				ToastUtil.toast(mContext, "删除失败");
			}
		});
	}

	private void showDeleteDialog(final int position){
		AlertUtils.deleteDialog(mContext,"是否删除该条下架物品",Integer.parseInt(NetUtils.getDisplayMetrics(mContext).get(NetUtils.DM_WIDTH)), new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				AlertUtils.dismiss();
				goodsControl(list.get(position).getGoods_id(), position);
			}
		});
	}
}
